import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    activeSys:{},
    activeMenu:"",
    drawerTitle:"",
    msgList:JSON.parse(window.localStorage.getItem('msgList'))||[],
    todoList:JSON.parse(window.localStorage.getItem('todoList'))||[],
  },
  mutations: {
    setActiveSys(state,item){
      if (typeof item != 'undefined'){
        state.activeSys = item
      }
    },
    setActiveMenu(state,path){
      if (typeof path != 'undefined'){
        state.activeMenu = path
      }
    },
    setDrawerTitle(state,title){
      if (typeof title != 'undefined'){
        state.drawerTitle = title
      }
    },
    pushMsgList(state,item){
      state.msgList.unshift(item)
      window.localStorage.setItem('msgList',JSON.stringify(state.msgList))
    },
    removeMsgList(state,item){
      state.msgList.slice(state.msgList.findIndex(o=>o==item),1)
      window.localStorage.setItem('msgList',JSON.stringify(state.msgList))
    },
    setMsgList(state,rows){
      state.msgList=rows
      window.localStorage.setItem('msgList',JSON.stringify(state.msgList))
    },
    pushTodoList(state,item){
      state.todoList.unshift(item)
      window.localStorage.setItem('todoList',JSON.stringify(state.todoList))
    },
    removeTodoList(state,item){
      state.todoList.slice(state.msgList.findIndex(o=>o==item),1)
      window.localStorage.setItem('todoList',JSON.stringify(state.todoList))
    },
    setTodoList(state,rows){
      state.todoList=rows
      window.localStorage.setItem('todoList',JSON.stringify(state.todoList))
    },
    setTodoFinished(state,value){
      const item = state.todoList.find(obj=>obj.id==value.id)
      if (typeof item != 'undefined'){
        item.finished = value.value
      }
    },
  },
  getters:{
    getTodoListNotFinished:state=>{
      return state.todoList.filter(todo=>!todo.finished)
    }
  },
  actions: {
  },
  modules: {
  }
})
